﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GenerationCode.CSClasses
{
    class ToControlCondition
    {


        private Utils.Table table;
        private string projectName;
        private string coder;
        private string email;
        private string objectClass;
  


        public ToControlCondition(Utils.Table tb, string projectName, string coder, string email)
        {
            this.table = tb;
            this.projectName = projectName;
            this.coder = coder;
            this.email = email;            
        }

        private string SetHeader()
        {
            string header = string.Format(@"/*
            Author : {0}
            Email  : {1}
            Date   : {2} 
          
            */
                using System;
            using System.Collections.Generic; 
            using System.Text;                            
            namespace {3}{4}
            /// <summary>
                    /// Define {5} object. This is condition param, that used add to where condition of sql query.
                /// </summary>
                public class {5} {4}",
                this.coder, this.email, DateTime.Now.ToShortDateString(), Utils.Utility.BusinessObjectNamespace, "{", this.table.Name + "ToControlCondition");
            return header;
        }

        private string SetProperties()
        {
            string properties = string.Format(@"
		#region properties"
                );
            foreach (Utils.Column col in this.table.Columns)
            {
                
                properties += string.Format(@"
          /// <summary>
                    /// Define {2} Property.
                /// </summary>
		public {0} {1} {3}
			get {3} return this.{2}; {4}
			set {3} this.{2} = value; {4}
		{4}",
                    col.DataType, Utils.Utility.ConvertToPropertyName(col.Name), col.Name, "{", "}");

            }
            properties += "\r\n" + @"
          /// <summary>
                    /// Define SortExpression Property.
                /// </summary>
        public string SortExpression
        {
            get
            {
                return this.sortExpression;
            }
            set
            {
                this.sortExpression = value;
            }
        }
         /// <summary>
                    /// Define SortField Property.
                /// </summary>
        public string SortField
        {
            get
            {
                return this.sortField;
            }
            set
            {
                this.sortField = value;
            }
        }
		#endregion properties";
            return properties;
        }

        private string SetFields()
        {
            string fields = @"
		#region fields";
            //set fields
            foreach (Utils.Column col in this.table.Columns)
            {

                fields += string.Format(@"
		private {0} {1}; ", col.DataType, col.Name);

            }
            fields += "\r\n  private string sortField;\r\n";
            fields += "\r\n  private string sortExpression;\r\n";
            fields += string.Format(@"
		
		#endregion fields");


            return fields;
        }

        private string SetConstructor()
        {
            string constructor = @"
		
		#region constructor";
            //first constructor
            constructor += string.Format(@"
          /// <summary>
            /// Define default constructor for {0} Object.
            /// </summary>
		public {0} (){1}", this.table.Name + "ToControlCondition", "{");
            foreach (Utils.Column col in this.table.Columns)
            {

                constructor += string.Format(@"
			this.{0} = {1};", col.Name, Utils.Utility.InitializeDataType(col.DataType));

            }
            constructor += "\r\nthis.sortExpression = string.Empty;\r\nthis.sortField = string.Empty;";
            constructor += @"
		}";
            //second constructor
            constructor += string.Format(@"
          /// <summary>
            /// Define constructor for {0} Object.
            /// </summary>
		public {0} ( ", this.table.Name + "ToControlCondition");
            foreach (Utils.Column col in this.table.Columns)
            {

                constructor += string.Format(@"{0} {1}, 
					", col.DataType, col.Name);

            }
            constructor = constructor.TrimEnd();
            constructor = constructor.Remove(constructor.Length - 1, 1) + " ) {";
            foreach (Utils.Column col in this.table.Columns)
            {

                constructor += string.Format(@"
			this.{0} = {0};", col.Name);

            }
            constructor += "\r\nthis.sortExpression = string.Empty;\r\nthis.sortField = string.Empty;";
            constructor += @"
		}";
            //  constructor += "111111111111111";
            //            constructor += string.Format(@" static {0}()
            //            {1}
            //              logger = LoggingUtility.GetLogger( typeof( {0} ).FullName );
            //            {2}", table.Name, "{", "}");
            //close constructor
            constructor += @"
		#endregion constructor";

            return constructor;

        }

        private string SetFooter()
        {
            string footer = @"
	}
}";
            return footer;
        }

        public string ObjectClass
        {
            get
            {
                this.objectClass = this.SetHeader() + this.SetFields() + this.SetConstructor() + this.SetProperties() + this.SetFooter();
                return this.objectClass;
            }
        }
    }
}
